package base

import (
	"git.mycaigou.com/gfyx/common/authorize"
	"git.mycaigou.com/gfyx/micro-gfyx-api/http_server/controller"
	"git.mycaigou.com/gfyx/micro-gfyx-api/infrastructure/common/errcode"
	"git.mycaigou.com/gfyx/micro-gfyx-api/infrastructure/dto"
	"git.mycaigou.com/gfyx/micro-gfyx-api/infrastructure/utils/jwt"
	"git.mycaigou.com/gfyx/micro-gfyx-api/service/base/user"
	"git.myscrm.cn/golang/stark/v4"
	"github.com/gin-gonic/gin"
)

type UserController struct {
	controller.BaseController
	userService user.UserSerivceIface
}

// NewUserController
// 构造函数
func NewUserController(userService user.UserSerivceIface) *UserController {
	return &UserController{userService: userService}
}

// RegisterRouter
// 注册路由
func (c *UserController) RegisterRouter(g *gin.RouterGroup) {
	// 获取用户信息
	g.GET("/info", c.GetUserInfo)
	// 单点登录子应用
	g.GET("/sso-subapp", c.SsoSubapp)
}

func (c *UserController) SsoSubapp(ctx *gin.Context) {
	//globalUserId := utils.GetGlobalUserId(ctx)
	tenantCode := authorize.GetTenantCode(ctx)
	userCode := authorize.GetUserCode(ctx)
	//fmt.Println("globalUserId==", globalUserId)
	source := ctx.Query("source")
	uri := ctx.Query("uri")
	if source == "" {
		c.Fail(ctx, errcode.COMMON_PARAMS_ERROR, "source不能为空")
		return
	}
	if uri == "" {
		c.Fail(ctx, errcode.COMMON_PARAMS_ERROR, "uri不能为空")
		return
	}

	if source == "gfyx" {
		//建模单点登录
		//建模跳转案例：https://toker.b2btst.com/PubPlatform/Nav/Login/SSO/Login?jwtToken=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiLkupHph4fotK0iLCJpYXQiOiIxNjgxODg5MTE1IiwianRpIjoiQjcxMDRFMDgtMzdEQi00MEQ2LTkwMTUtQUQ3REM1M0UxQTkxIiwidSI6ImFkbWluIiwiYSI6InpodXlpbmd5b25nIiwidCI6Im15c29mdCJ9.s9b1GZlHzJq9UPUNYs6uCYMfGApPfqvs7A_ISQd_gtE&PageUrl=/std/40220901/08dac22e-243f-4333-842f-6af54a2ed2e7?_mp=empty
		//tenantCode := "mysoft"
		//tenantUserCode := "admin"
		token, err := jwt.DefaultJWT.GenerateGfyxToken(tenantCode, userCode)
		if err != nil {
			c.Fail(ctx, errcode.COMMON_ERROR, err.Error())
		}
		// 生成sso地址
		//https://toker.b2btst.com/PubPlatform/Nav/Login/SSO/Login?jwtToken=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiLkupHph4fotK0iLCJpYXQiOiIxNjgxODg5MTE1IiwianRpIjoiQjcxMDRFMDgtMzdEQi00MEQ2LTkwMTUtQUQ3REM1M0UxQTkxIiwidSI6ImFkbWluIiwiYSI6InpodXlpbmd5b25nIiwidCI6Im15c29mdCJ9.s9b1GZlHzJq9UPUNYs6uCYMfGApPfqvs7A_ISQd_gtE&PageUrl=/std/40220901/08dac22e-243f-4333-842f-6af54a2ed2e7?_mp=empty
		//获取gfyx建模地址
		gfyxDomain := stark.Configer.(stark.ApolloConfigerIface).GetStringValue("gfyx_model_domain", "")
		// gfyxDomain := "https://toker.b2btst.com"
		ssoUrl := gfyxDomain + "/PubPlatform/Nav/Login/SSO/Login" + "?" + "jwtToken=" + token + "&PageUrl=" + uri
		sso := dto.Sso{
			SsoUrl: ssoUrl,
		}
		//fmt.Println("sso==", sso) a
		c.Success(ctx, sso)
	} else if source == "ycg" {

	} else {
		c.Fail(ctx, errcode.COMMON_PARAMS_ERROR, "来源不支持")
		return
	}
}

func (c *UserController) GetUserInfo(ctx *gin.Context) {
	globalUserId := authorize.GetGlobalUserId(ctx)
	if globalUserId == "" {
		c.Fail(ctx, errcode.COMMON_PARAMS_ERROR, "用户uid为空")
		return
	}

	//globalUserIdStr, _ := unionId.(string)
	//if globalUserIdStr == "" {
	//	c.Fail(ctx, errcode.COMMON_PARAMS_ERROR, "用户编号为空")
	//	return
	//}

	userInfo, err := c.userService.GetUserInfoByGlobalUserId(ctx, globalUserId)

	//response := user.UserLoginInfo{
	//	UserInfo: &user.UserInfo{
	//		UserName:   "测试用户名",
	//		Uid:        "1aa8fc77-0ca4-477d-ada6-256a0bd593e9",
	//		UserCode:   "admin",
	//		TenantCode: "mysoft",
	//		TenantName: "admin租户名",
	//		Logo:       "https://xxxx.jpg",
	//		Mobile:     "15218621431",
	//	},
	//	Token:    "71fdd198-a1af-492c-b374-8edac000525f",
	//}

	if err != nil {
		stark.Logger.Errorf(ctx, "GetUserInfo-error: %+v, userid: %s", err.Error(), globalUserId)
		c.Fail(ctx, errcode.COMMON_DATA_NOT_EXIST, err.Error())
		return
	}

	c.Success(ctx, userInfo)
}
